package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //根据用户判断传入的套餐或者菜品购物车是否有,传入的DTO没有用户id。不能删别人购物车里的商品
        //ShoppingCart里的name是套餐或者菜品的名字（有谁的id是谁的），两个id只能有一个存在，购物车一次只加一件商品
        ShoppingCart shoppingCart = new ShoppingCart();
        //这里会把口味值赋值给shoppingCart，套餐或者菜品id也会过去（后面往shoppingCart赋值就不再写了，除非换一个对象）。
        //然后进行查询购物车是否有（userId+套餐id/菜品id+口味必能确定一个购物车物品。重复会个数+1）
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //从线程变量传入登录用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询数据库（用集合接收，这里查询结果是一条数据。但是会有根据用户查询他购物车内容，所以用List）
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShoppingCartList(shoppingCart);
        //如果不为空就购物车里数量加一
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //获得这个元素，就一个，也就是0索引
            shoppingCart = shoppingCartList.get(0);
            //将个数加一
            shoppingCart.setNumber(shoppingCart.getNumber() + 1);
            //执行购物车的update语句。可以与其他更新语句共用
            shoppingCartMapper.update(shoppingCart);
        } else {
            //如果购物车里没有就插入，并判断是套餐还是菜品
            //如果是套餐
            if (shoppingCartDTO.getSetmealId() != null) {
                //查询出这个套餐内容
                Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
                //给购物车对象赋值，因为变量名不对照，不能使用工具类了
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
            } else {
                //如果是菜品
                Dish dish = dishMapper.getById(shoppingCartDTO.getDishId());
                //给购物车对象赋值
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }
            //增加隐式属性
            //个数很重要
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        //创建购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        //查看的只有当前登录用户的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.getShoppingCartList(shoppingCart);
        return list;
    }


    /**
     * 情况购物车
     */
    @Override
    public void clean() {
        //可以直接情况购物车了，清空的是当前登录用户的
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     * 删除购物车一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //创建一个购物车对象，调用那个共用SQL
        ShoppingCart shoppingCart = new ShoppingCart();
        //赋值
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //制定查询的是哪个用户
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //调用Mapper层根据对象查询购物车对象（这里只会查到一条，是因为用了共用SQL返回值才是集合）
        //userId+（套餐或者菜品）id+口味能确定唯一值
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShoppingCartList(shoppingCart);
        //获得这个元素，其中包含这条数据的购物车的id主键
        shoppingCart = shoppingCartList.get(0);
        //判断查到的元素个数是否为1
        if (shoppingCart.getNumber() == 1) {
            //删除这个购物车对象By购物车id，根据别的怪麻烦，还要动态SQL
            shoppingCartMapper.deleteByShoppingCartId(shoppingCart.getId());
        } else {
            //个数不唯一，数量就减一
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            //执行更新语句
            shoppingCartMapper.update(shoppingCart);
        }
    }
}
